package com.ruikong.dazhi.controller.basicinfo;


import com.github.pagehelper.PageInfo;
import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.basicinfo.ProductRecipe;
import com.ruikong.dazhi.model.input.ProductRecipePageInput;
import com.ruikong.dazhi.service.basicinfo.IProductRecipeService;
import com.ruikong.dazhi.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.EscapedErrors;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.basic.BasicEditorPaneUI;

@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "产品管理")
public class ProductRecipeController {

    @Resource
    private IProductRecipeService iProductRecipeService;


    private final  static Logger logger= LoggerFactory.getLogger(ProductRecipeController.class);



    @PostMapping("/getRecipePageInfo")
    @ApiOperation("产品配方-分页")
    public PageInfo getRecipePageInfo(@RequestBody ProductRecipePageInput input){
        PageInfo pageInfo=null;
        try {
            pageInfo=this.iProductRecipeService.getRecipePageInfo(input);
        }catch (Exception e){
            logger.error("getRecipePageInfo{}",e.getMessage());
            e.printStackTrace();
        }
        return pageInfo;
    }




    @PostMapping("/addRecipe")
    @ApiOperation("产品配方-新增")
    public BaseResult addRecipe(@RequestBody ProductRecipe productRecipe){
        BaseResult baseResult=new BaseResult();
        try {
            if (this.iProductRecipeService.insert(productRecipe)>0){
                baseResult.setMsg(ResultApi.addSuccessMsg);
                baseResult.setCode(ResultApi.successCode);
            }
            else{
                baseResult.setCode(ResultApi.errorCode);
                baseResult.setMsg(ResultApi.addFailMsg);
            }
        }catch (Exception e){
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.addFailMsg);
            logger.error("addRecipe{}",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }



    @PostMapping("/updateRecipe")
    @ApiOperation("产品配方-编辑")
    public BaseResult updateRecipe(@RequestBody ProductRecipe productRecipe){
        BaseResult baseResult=new BaseResult();
        try {
            if (this.iProductRecipeService.update(productRecipe)>0){
                baseResult.setMsg(ResultApi.updateSuccessMsg);
                baseResult.setCode(ResultApi.successCode);
            }
            else{
                baseResult.setCode(ResultApi.errorCode);
                baseResult.setMsg(ResultApi.updateFailMsg);
            }
        }catch (Exception e){
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.updateFailMsg);
            logger.error("updateRecipe{}",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }



    @PostMapping("/deleteRecipe")
    @ApiOperation("产品配方-删除")
    public BaseResult deleteRecipe(@RequestBody ProductRecipe productRecipe){
        BaseResult baseResult=new BaseResult();
        try {
            if(this.iProductRecipeService.deleteById(productRecipe.getId())){
                baseResult.setMsg(ResultApi.deleteSuccessMsg);
                baseResult.setCode(ResultApi.successCode);
            }
            else {
                baseResult.setCode(ResultApi.errorCode);
                baseResult.setMsg(ResultApi.deleteFailMsg);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("deleteRecipe{}",e.getMessage());
            baseResult.setCode(ResultApi.errorCode);
            baseResult.setMsg(ResultApi.deleteFailMsg);
        }
        return baseResult;
    }


    @PostMapping("/queryRecipeById")
    @ApiOperation("根据id查询配方数据")
    public BaseResult queryRecipeById(@RequestBody ProductRecipe productRecipe){
        BaseResult baseResult=new BaseResult();
        try {
            productRecipe=this.iProductRecipeService.queryById(productRecipe.getId());
            baseResult.setCode(ResultApi.successCode);
            baseResult.setData(productRecipe);
        }catch (Exception e){
            baseResult.setCode(ResultApi.failCode);
            baseResult.setMsg(ResultApi.operateFailMsg);
        }
        return baseResult;
    }



    @GetMapping("/downRecipeExcelTemplate")
    @ApiOperation("配方导入模版下载")
    public void downRecipeExcelTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) {
        String[] columNames = {"产品号", "原料编码", "占比（%）","误差值（kg）不填默认为2"};
        ExcelUtil.download(response, request, fileName, columNames);
    }


    @PostMapping("/recipeImport")
    @ApiOperation("产品配方导入")
    public BaseResult recipeImport(@RequestParam("file") MultipartFile multipartFile) {
        BaseResult baseResult = new BaseResult();
        try {
            baseResult = this.iProductRecipeService.recipeImport(multipartFile);
        } catch (Exception e) {
            logger.error("recipeImport", e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }






}
